package com.zlc.common.core.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlc.common.common.service.BaseServiceImpl;
import com.zlc.common.core.process.model.entity.StoneEventConfig;
import com.zlc.common.core.process.model.entity.StoneProcessAuthConfig;
import com.zlc.common.core.process.model.entity.StoneProcessVariableConfig;
import com.zlc.common.core.process.mapper.StoneProcessVariableConfigMapper;
import com.zlc.common.core.process.model.vo.StoneEventConfigVo;
import com.zlc.common.core.process.model.vo.StoneProcessVariableConfigVo;
import com.zlc.common.core.process.service.StoneProcessVariableConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.zlc.common.common.constant.WorkflowConstant.TYPE_PROCESS;

/**
 * @author  zl
 * @date 2023-10-17 
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StoneProcessVariableConfigServiceImpl extends BaseServiceImpl<StoneProcessVariableConfigMapper, StoneProcessVariableConfig, StoneProcessVariableConfigVo> implements StoneProcessVariableConfigService{
    @Override
    public void removeByProcessId(String processId) {
        LambdaQueryWrapper<StoneProcessVariableConfig> wrapper = wrapper()
                .eq(StoneProcessVariableConfig::getProcessId, processId);
        List<StoneProcessVariableConfigVo> nodeVos = queryVoList(wrapper);
        if(!ObjectUtils.isEmpty(nodeVos)){
            List<String> ids = nodeVos.stream().map(StoneProcessVariableConfigVo::getId).collect(Collectors.toList());
            deleteObjByIds(ids);
        }
    }

    @Override
    public List<StoneProcessVariableConfigVo> getProcessVariables(String processId) {
        LambdaQueryWrapper<StoneProcessVariableConfig> wrapper = wrapper()
                .eq(StoneProcessVariableConfig::getProcessId, processId)
                .orderByAsc(StoneProcessVariableConfig::getOrder);
        return queryVoList(wrapper);
    }

    @Override
    public List<StoneProcessVariableConfigVo> getNodeVariables(String processId, String nodeId) {
        LambdaQueryWrapper<StoneProcessVariableConfig> wrapper = wrapper()
                .eq(StoneProcessVariableConfig::getProcessId, processId)
                .eq(StoneProcessVariableConfig::getNodeId, nodeId)
                .orderByAsc(StoneProcessVariableConfig::getOrder);
        return queryVoList(wrapper);
    }


    public Map<String,String> getVariableConfigs(String processId, String nodeId){
        List<StoneProcessVariableConfigVo> allVariable = getProcessVariables(processId);
        List<StoneProcessVariableConfigVo> nodeVariables = allVariable.stream().filter(v -> v.getNodeId() != null && v.getNodeId().equals(nodeId)).collect(Collectors.toList());
        List<StoneProcessVariableConfigVo> processVariables = allVariable.stream().filter(a -> ObjectUtils.isEmpty(a.getNodeId())).collect(Collectors.toList());
        if(!ObjectUtils.isEmpty(processVariables)){
            nodeVariables.addAll(processVariables);
        }
        return nodeVariables.stream().collect(Collectors.toMap(StoneProcessVariableConfigVo::getKey,StoneProcessVariableConfigVo::getValue));
    }
}
